<!-- 人员选择控件 liang -->
<template>
  <el-dialog
    :title="dialogTiltle"
    :visible.sync="dialogVisible"
    class="dialogStyleMiddle"
    :before-close="cancel"
  >
    <el-row :gutter="20">
      <!--部门数据 -->
      <el-col :span="4" :xs="24">
        <div class="head-container">
          <el-input
            v-model="deptName"
            placeholder="请输入部门名称"
            clearable
            size="small"
            prefix-icon="el-icon-search"
            style="margin-bottom: 20px"
          />
        </div>
        <div class="head-container">
          <el-tree
            ref="tree"
            :data="deptOptions"
            :props="defaultProps"
            :expand-on-click-node="false"
            :filter-node-method="filterNode"
            default-expand-all
            @node-click="handleNodeClick"
          />
        </div>
      </el-col>
      <!-- 用户数据 -->
      <el-col :span="20" :xs="24">
        <el-form ref="queryForm" :inline="true" label-width="80px" :model="filters" class="dialog-form">
          <el-form-item label="用户名称" prop="loginName">
            <el-input
              v-model="filters.loginName"
              size="small"
              placeholder
              clearable
              prefix-icon="el-icon-search"
            />
          </el-form-item>
          <el-form-item label="状态" prop="status">
            <label slot="label">状&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;态</label>
            <el-select
              v-model="filters.status"
              size="small"
              filterable
              placeholder="请选择"
              prop="status"
            >
              <el-option
                v-for="item in statusOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item class="button-wrap">
            <el-button
              size="small"
              type="primary"
              style="margin-left: 5px"
              icon="el-icon-search"
              @click="handleQuery()"
            >搜索</el-button>
            <el-button
              size="small"
              icon="el-icon-refresh"
              @click="resetQuery()"
            >重置</el-button>
          </el-form-item>
        </el-form>
        <!-- 表格展示start -->
        <el-table
          ref="multipleTable"
          v-loading="loading"
          :data="sysUserData"
          stripe
          class="table-list"
          tooltip-effect="dark"
          :row-key="getRowKey"
          size="mini"
          @selection-change="handleSelectionChange"
          @row-click="rowChecked"
        >
          <el-table-column type="selection" :reserve-selection="true" width="38" align="center" />
          <el-table-column
            prop="loginName"
            label="用户名称"
            align="center"
          />
          <el-table-column
            prop="userName"
            label="用户昵称"
            align="center"
          />
          <el-table-column prop="status" label="状态" align="center">
            <template slot-scope="scope">
              <span v-if="scope.row.status=== '0'">正常</span>
              <span v-if="scope.row.status=== '1'">停用</span>
            </template>
          </el-table-column>
          <el-table-column prop="createTime" label="创建时间" align="center" />
        </el-table>
        <!-- 表格展示end-->
        <!-- 分页-->
        <pagination
          :total="total"
          :page.sync="filters.pageNum"
          :limit.sync="filters.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>
    <span slot="footer" class="dialog-footer">
      <el-button size="medium" @click="cancel">取 消</el-button>
      <el-button size="medium" type="primary" @click="submitForm">确 定</el-button>
    </span>
  </el-dialog>
</template>
<script>
// addRoleToUser
// 获取部门菜单树
import { selectAllCompanyDeptTree } from '@/api/system/dept'
// 引入用户列表
import { selectAllCompanyUserNoManagerList } from '@/api/system/user'
export default {
  // 接受父组件传过来的值
  props: {
    userList: {
      type: Array,
      // 当为数组类型设置默认值时必须使用数组返回
      default: function() {
        return []
      }
    }
  },
  data() {
    return {
      dialogTiltle: '',
      dialogVisible: false,
      sysUserData: [],
      // 部门名称
      deptName: undefined,
      // 部门树选项
      deptOptions: undefined,
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      loading: false,
      // 选中表格对话框的存储的值
      multipleSelection: [],
      // 查询参数
      filters: {
        pageNum: 1,
        pageSize: this.fristPageSize,
        deptId: '',
        loginName: '',
        phonenumber: '',
        status: ''
      },
      // 分页总条数
      total: 0,
      statusOptions: [
        { value: '1', label: '停用' },
        { value: '0', label: '正常' }
      ]
    }
  },
  watch: {
    // 根据名称筛选部门树
    deptName(val) {
      this.$refs.tree.filter(val)
    }
  },
  methods: {
    showUser() {
      this.dialogTiltle = '接收人选择'
      this.dialogVisible = true
      this.selectDeptTree()
      this.getList()
    },
    // 单击每行时每行的复选框选中
    rowChecked(row) {
      this.$refs.multipleTable.toggleRowSelection(row)
    },
    // 分页选中返回数据
    getRowKey(row) {
      return row.userId
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true
      return data.label.indexOf(value) !== -1
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.filters.deptId = data.id
      this.btnSearch()
    },
    // 点击表格前面选框时触发的事件
    handleSelectionChange(val) {
      this.multipleSelection = val
      // this.checkIds(val)
    },
    // 把选中的人员id 和 人员集合传输到父节点
    checkIds(val) {
      var ids = ''
      for (var i = 0; i < val.length; i++) {
        ids += val[i].userId + ','
      }
      this.$emit('multipleSelection', ids, this.multipleSelection)
    },
    getList() {
      this.loadUserDate()
    },

    loadUserDate: function() {
      var _this = this
      selectAllCompanyUserNoManagerList(this.filters).then(resp => {
        if (resp) {
          _this.sysUserData = resp.data.rows
          _this.total = resp.data.total
          this.$nextTick(() => {
            // 回显选中的行数
            this.echo(this.userList)
          })
        } else {
          this.msgError('查找失败！')
        }
      })
    },
    // 回显数据
    echo: function(userList) {
      // 获取父组件中 封装自定义行和列的信息
      if (userList != null && userList.length > 0) {
        userList.forEach(row => {
          this.$refs.multipleTable.toggleRowSelection(row)
        })
      } else {
        this.emptmultipleSelection()
      }
    },
    // 搜索方法
    btnSearch() {
      this.loadUserDate()
    },
    // 清空搜索信息
    resetForm() {
      this.filters = {
        loginName: '',
        phonenumber: '',
        status: '',
        dateRange: ''
      }
    },
    // 重置
    btnReset() {
      this.resetForm()
      this.loadUserDate()
    },
    // 部门选择树列表
    selectDeptTree() {
      var _this = this
      selectAllCompanyDeptTree().then(resp => {
        if (resp) {
          _this.deptOptions = resp.data
        }
      })
    },
    // 清空勾选的行
    emptmultipleSelection() {
      this.$refs.multipleTable.clearSelection()
    },
    // 角色添加用户确认按钮
    submitForm() {
      // 调用向父组件传值的方法
      this.checkIds(this.multipleSelection)
      this.dialogVisible = false
    },
    // 取消
    cancel() {
      var _this = this
      _this.dialogVisible = false
      // 清空勾选的行数
      this.$refs.multipleTable.toggleAllSelection()
    }

  }
}
</script>
